package com.gg.web;

import com.gg.pojo.*;
import com.gg.service.OrderService;
import com.gg.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet{
    OrderService orderService = new OrderServiceImpl();
    /**
     * 购物车点击结账生成订单功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取Cart购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取Userid
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser==null){
            //为空说明没有登录，请求转发到登录页面,并return不生产订单
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        //System.out.println("OrderServlet程序在【"+Thread.currentThread().getName()+"】中"); //测试

        Integer userId = loginUser.getId();//获取用户ID
        //调用orderService.createOrder(Cart,Userid);生成订单
        String orderId = orderService.createOrder(cart, userId);
        //req.setAttribute("orderId", orderId);
        // 请求转发到/pages/cart/checkout.jsp //存在刷新重复提交BUG，得用重定向
        //req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");

       /* try { //不需要 事务Filter.java文件
            String orderId = orderService.createOrder(cart, userId);
            JdbcUtils.commitAndClose();//提交事务
            req.setAttribute("orderId",orderId);
            req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        } catch (Exception e) {
            JdbcUtils.rollbackAndClose();//回滚事务
            e.getMessage();
            req.setAttribute("msg",e.getMessage());
            req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);

        }*/

    }

    //订单管理页面显示数据库中的所有订单
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过OrderService查询全部订单
        List<Order> orders = orderService.showAllOrders();
        //2.把全部图书存储到Request域中
        req.setAttribute("orders",orders);
        //3.请求转发到/pages/manager/order_manager.jsp页面,同时把数据带过去
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);
    }

    //发货功能
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数orderId
        String orderId = req.getParameter("orderId");
        //调用orderService.sendOrder(orderId); 进行发货
        orderService.sendOrder(orderId);
        //3.重定向回订单管理列表页面，并途径OrderServlet调用showAllOrders()刷新
        resp.sendRedirect(req.getContextPath()+ "/orderServlet?action=showAllOrders");
    }

    //管理端查看订单详情功能
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数orderId和userId（userId只用于查看详情展示）
        String orderId = req.getParameter("orderId");
        String userId = req.getParameter("userId");
        //调用orderService.showOrderDetail(orderId); 返回相应的订单项
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //将订单项保存到Request域中
        req.setAttribute("orderItems",orderItems);
        req.setAttribute("userId",userId);
        //3.请求转发到详情页面
        req.getRequestDispatcher("/pages/manager/order_detail.jsp").forward(req,resp);
    }

    //客户端查看订单详情功能
    protected void showOrderDetailOfClient(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数orderId
        String orderId = req.getParameter("orderId");
        //调用orderService.showOrderDetail(orderId); 返回相应的订单项
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //将订单项保存到Request域中
        req.setAttribute("orderItems",orderItems);
        //3.请求转发到详情页面
        req.getRequestDispatcher("/pages/order/order_detail.jsp").forward(req,resp);
    }

    //点击我的订单查看用户的所有订单
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取Session中的user
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        //获取userId
        Integer userId = user.getId();
        //调用orderService.showMyOrders(userId); 获得订单集合
        List<Order> myOrders = orderService.showMyOrders(userId);
        //保存到Request域
        req.setAttribute("myOrders",myOrders);
        //请求转发到我的订单页面
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);
    }

}
